package com.aeye.mbr.upms.rpc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.aeye.mbr.common.result.BootstrapTableParams;
import com.aeye.mbr.common.result.BootstrapTableResult;
import com.aeye.mbr.common.plugin.mapper.BaseServiceImpl;
import com.aeye.mbr.upms.rpc.mapper.UpmsLogMapper;
import com.aeye.mbr.upms.dao.model.UpmsLog;
import com.aeye.mbr.upms.rpc.api.UpmsLogService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * UpmsLogService实现
 * Created by shuaeye.mbr on 2017/3/20.
 */
@Service
@Transactional
public class UpmsLogServiceImpl extends BaseServiceImpl<UpmsLog> implements UpmsLogService {

    private static Logger _log = LoggerFactory.getLogger(UpmsLogServiceImpl.class);

    @Autowired
    UpmsLogMapper upmsLogMapper;


    @Override
    public BootstrapTableResult<UpmsLog> selectUpmsLogByParams(BootstrapTableParams params) {
        BootstrapTableResult result = new BootstrapTableResult();
        if (params != null) {
            PageHelper.startPage(params.getPage(), params.getLimit());
            Example example = new Example(UpmsLog.class);
            String sort = params.getSort();
            String order = params.getOrder();
            String search = params.getSearch();
            if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
                example.setOrderByClause(sort + " " + order);
            }
            if (StringUtils.isNotBlank(search)) {
                example.or().andLike("description", "%" + search + "%");
            }
            List<UpmsLog> users = upmsLogMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(users)) {
                if (users instanceof Page) {
                    Page<UpmsLog> pls = (Page) users;
                    result.setTotal((int) pls.getTotal());
                    result.setRows(pls.getResult());
                } else {
                    result.setTotal(users.size());
                    result.setRows(users);
                }
            }
        }
        return result;
    }
}